package demo1109.demo02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/9 16:02
 */
public class ProxyFactory {

    private FangDong target;

    public ProxyFactory(FangDong target) {
        this.target = target;
    }

    public Chuzu getInstance(){

        ClassLoader loader = target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        InvocationHandler h = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                intro();
                Object result = method.invoke(target, args);
                watch();
                return result;
            }
        };
        Object o = Proxy.newProxyInstance(loader, interfaces, h);

        return (Chuzu) o;
    }
    public void watch(){
        System.out.println("看房");
    }
    public void intro(){
        System.out.println("房源介绍");
    }

}
